package com.jac.hellojac;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;
import com.jac.hellojac.SessionEvents.AuthListener;
import com.jac.hellojac.SessionEvents.LogoutListener;

public class HomeActivity extends Activity {

	public static final String APP_ID = "428294653890486";
	TextView tv_SaySomething;
	private LoginButton mLoginButton;

	private Facebook mFacebook;
	private AsyncFacebookRunner mAsyncRunner;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (APP_ID == null) {
			Util.showAlert(this, "Warning", "Facebook Applicaton ID must be "
					+ "specified before running this example: see Example.java");
		}

		setContentView(R.layout.activity_home);

		mLoginButton = (LoginButton) findViewById(R.id.login);

		mFacebook = new Facebook(APP_ID);
		mAsyncRunner = new AsyncFacebookRunner(mFacebook);

		SessionStore.restore(mFacebook, this);
		SessionEvents.addAuthListener(new SampleAuthListener());
		SessionEvents.addLogoutListener(new SampleLogoutListener());
		mLoginButton.init(this, mFacebook);

		Button btn_SaySomething = (Button) findViewById(R.id.btn_SayHello);
		tv_SaySomething = (TextView) findViewById(R.id.tv_SaySomething);

		btn_SaySomething.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				tv_SaySomething.setText("Hello JAC!");
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_home, menu);
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		mFacebook.authorizeCallback(requestCode, resultCode, data);
	}

	public class SampleAuthListener implements AuthListener {

		public void onAuthSucceed() {
			tv_SaySomething.setText("You have logged in!");
			//mRequestButton.setVisibility(View.VISIBLE);

			mAsyncRunner.request("me", new SampleRequestListener());
		}

		public void onAuthFail(String error) {
			tv_SaySomething.setText("Login Failed: " + error);
		}
	}

	public class SampleLogoutListener implements LogoutListener {
		public void onLogoutBegin() {
			tv_SaySomething.setText("Logging out...");
		}

		public void onLogoutFinish() {
			tv_SaySomething.setText("You have logged out! ");
		}
	}

	public class SampleRequestListener extends BaseRequestListener {

		public void onComplete(final String response, final Object state) {
			try {
				Log.d("Facebook-Example", "Response: " + response.toString());
				JSONObject json = Util.parseJson(response);
				final String name = json.getString("name");

				HomeActivity.this.runOnUiThread(new Runnable() {
					public void run() 
					{
						tv_SaySomething.setText("Hello there, " + name + "!");
						startActivity(new Intent(HomeActivity.this, SecondActivity.class).putExtra("username", name));
					}
				});
			} catch (JSONException e) {
				Log.w("Facebook-Example", "JSON Error in response");
			} catch (FacebookError e) {
				Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
			}
		}
	}

}
